﻿//************************************************************************//
//文件功能：操作INI
//创 建 人：薄帅
//创建时间：2011年05月26日
//修 改 人：
//修改时间：
//************************************************************************//

using System;
using System.Collections.Generic;
using System.Text;

namespace ProjBackEnd.Common.Helper
{
    public sealed class OperateINI
    {
        /// <summary>
        /// 读取INI文件
        /// </summary>
        /// <param name="filename">文件名</param>
        /// <param name="section">节点</param>
        /// <param name="key">键名</param>
        /// <returns>返回键值，如果没有找到，则返回NONE</returns>
        public static string GetKeyString(string filename, string section, string key)
        {
            string def = "NONE";
            StringBuilder temp = new StringBuilder(255);
            Api.GetPrivateProfileString(section, key, def, temp, 255, filename);
            return temp.ToString();
        }
        
        /// <summary>
        /// 写INI文件
        /// </summary>
        /// <param name="filename">文件名</param>
        /// <param name="section">节点</param>
        /// <param name="key">键名</param>
        /// <param name="strVal">键值</param>
        /// <returns></returns>
        public static bool WriteString(string filename, string section, string key, string strVal)
        {
            if (Api.WritePrivateProfileString(section, key, strVal, filename) == 0)
                return false;
            else
                return true;
        }
        /// <summary>
        /// 删除键名
        /// </summary>
        /// <param name="filename">文件名</param>
        /// <param name="section">节点</param>
        /// <param name="key">键名</param>
        /// <returns></returns>
        public bool DelKey(string filename,string section, string key )
        {
            if (Api.WritePrivateProfileString(section, key, null, filename) == 0)
                return false;
            else
                return true;
        }
        /// <summary>
        /// 删除节点
        /// </summary>
        /// <param name="filename">文件名</param>
        /// <param name="section">节点</param>
        /// <returns></returns>
        public bool DelSection(string filename, string section)
        {
            if (Api.WritePrivateProfileString(section, null, null, filename) == 0)
                return false;
            else
                return true;
        }


    }
}
